﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using UCL.Powersource.Analysis;

namespace UCL.Powersource.Statistics
{
    [Export(typeof(IStatistic))]
    [DataContract(IsReference = true)]
    public class PercentageOfPlannedEffortDone : EffortStatisticsBase
    {
        [DataMember]
        private readonly string _QueryString = "Select [ID], [Iteration Path], [State], [Effort] From WorkItems Where [Work Item Type] = @WIType AsOf @AsOf";       // Declaration of the _QueryString which returns work item ID, iteration path, state and effort

        protected override string QueryString { get { return _QueryString; } }

        [DataMember]
        private readonly string _WIType = "Product Backlog Item";       // Declaration of Product Backlog Item

        protected override string WIType { get { return _WIType; } }

        [DataMember]
        private string _State = "Done";        // State of the work item is Done

        protected override string State { get { return _State; } }

        [DataMember]
        private readonly string _WasEver = "";      // State of the work item which was before

        protected override string WasEver { get { return _WasEver; } }

        [ExcludeFromCodeCoverage]
        public override string Label
        {
            get { return "Percentage Of Planned Effort Done"; }
        }

        [ExcludeFromCodeCoverage]
        public override string Description
        {
            get { return "The percentage of planned effort that was with state Done in the end of iteration"; }
        }

        [ExcludeFromCodeCoverage]
        public override string Name
        {
            get { return "PercentageOfPlannedEffortDone"; }
        }

        public static readonly Guid CN_Statistic_Guid = new Guid("4E5E72F2-3AD4-445B-B25D-C8C9F26D6E7B");
        [ExcludeFromCodeCoverage]
        public override Guid Statistic_Guid
        {
            get { return CN_Statistic_Guid; }
        }

        [DataMember]
        private string _FormatString = "p";

        public override string FormatString
        {
            get
            {
                return _FormatString;
            }
        }

        protected override bool ValidState(WorkItem item)
        {
            if (State == "Done")
            return (string)item.Fields["State"].Value == State;
            return (string)item.Fields["State"].Value != State;

        }

        protected override double? CalculateStatistic(ITfsTeamProjectConfiguration teamProjConfig, ITimeFrame timeFrame, string qString)
        {
            double? done = base.CalculateStatistic(teamProjConfig, timeFrame, qString);
            _State = "Removed";
            double? planned = base.CalculateStatistic(teamProjConfig, timeFrame, qString);

            if (planned == null || done == null) return null;
            if (planned == 0) return null;

            return (done/planned);
        }
    }
}
